package cn.angis.flow.service;

import cn.angis.db.model.DtoInput;
import cn.angis.db.model.PageModel;
import cn.angis.db.model.PageOfResult;
import cn.angis.flow.mapper.WarmFlowMapper;
import cn.angis.flow.vo.FlowTaskVo;
import cn.angis.flow.vo.WarmFlowInteractiveTypeVo;
import cn.angis.system.dto.input.DeptInput;
import cn.angis.system.dto.input.UserInput;
import cn.angis.system.dto.output.DeptOutput;
import cn.angis.system.dto.output.UserOutput;
import cn.angis.system.model.AngisUser;
import cn.angis.system.model.Role;
import cn.angis.system.service.AngisUserService;
import cn.angis.system.service.DeptService;
import cn.angis.system.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.page.DefaultPageRequest;
import org.beetl.sql.core.page.PageRequest;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.solon.annotation.Db;
import org.dromara.warm.flow.core.entity.HisTask;
import org.dromara.warm.flow.core.entity.Task;
import org.dromara.warm.flow.core.utils.CollUtil;
import org.dromara.warm.flow.core.utils.MapUtil;
import org.dromara.warm.flow.core.utils.StreamUtils;
import org.dromara.warm.flow.orm.entity.FlowHisTask;
import org.dromara.warm.flow.orm.entity.FlowTask;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 流程执行SERVICEIMPL
 *
 * @author WARM
 * @since 2023/5/29 13:09
 */
@Component
public class ExecuteService {

    @Db
    private WarmFlowMapper flowMapper;
    @Inject
    private DeptService deptService;

    @Inject
    private RoleService roleService;
    @Inject
    private AngisUserService angisUserService;

    public PageOfResult<FlowTaskVo> toDoPage(FlowTask task, PageModel pageModel) {
        PageOfResult<FlowTaskVo> pageOfResult = new PageOfResult<>();
        PageRequest request = DefaultPageRequest.of(pageModel.getCurrent(),pageModel.getSize());
        PageResult result = flowMapper.toDoPage(request, task);
        pageOfResult.setCurrent(pageModel.getCurrent());
        pageOfResult.setPages(result.getTotalPage());
        pageOfResult.setSize(pageModel.getSize());
        pageOfResult.setTotal(result.getTotalRow());
        pageOfResult.setRecords(result.getList());
        return pageOfResult;
    }

    public PageOfResult<FlowHisTask> donePage(HisTask hisTask, PageModel pageModel) {
        PageOfResult<FlowHisTask> pageOfResult = new PageOfResult<>();
        PageRequest request = DefaultPageRequest.of(pageModel.getCurrent(),pageModel.getSize());
        PageResult result = flowMapper.donePage(request, hisTask);
        pageOfResult.setCurrent(pageModel.getCurrent());
        pageOfResult.setPages(result.getTotalPage());
        pageOfResult.setSize(pageModel.getSize());
        pageOfResult.setTotal(result.getTotalRow());
        pageOfResult.setRecords(result.getList());
        return pageOfResult;
    }

    public PageOfResult<FlowHisTask> copyPage(FlowTask flowTask, PageModel pageModel) {
        PageOfResult<FlowHisTask> pageOfResult = new PageOfResult<>();
        PageRequest request = DefaultPageRequest.of(pageModel.getCurrent(), pageModel.getSize());
        PageResult result = flowMapper.copyPage(request, flowTask);
        pageOfResult.setCurrent(pageModel.getCurrent());
        pageOfResult.setPages(result.getTotalPage());
        pageOfResult.setSize(pageModel.getSize());
        pageOfResult.setTotal(result.getTotalRow());
        pageOfResult.setRecords(result.getList());
        return pageOfResult;
    }

    /**
     * 根据ID反显姓名
     *
     * @param ids 需要反显姓名的用户ID
     * @return {@link List<  UserOutput  >}
     * @author liangli
     * @date 2024/8/21 17:11
     **/
    public List<UserOutput> idReverseDisplayName(Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            return null;
        }
        return flowMapper.idReverseDisplayName(ids);
    }

    /**
     * 查询不包含输入的所有用户
     *
     * @param warmFlowInteractiveTypeVo 输入用户编号集合
     * @return 用户列表
     */
    public List<UserOutput> selectNotUserList(WarmFlowInteractiveTypeVo warmFlowInteractiveTypeVo) {
        return flowMapper.selectNotUserIds(warmFlowInteractiveTypeVo);
    }

    /**
     * 查询包含输入的所有用户
     *
     * @param warmFlowInteractiveTypeVo 输入用户编号集合
     * @return 用户列表
     */
    public List<UserOutput> selectUserList(WarmFlowInteractiveTypeVo warmFlowInteractiveTypeVo) {
        return flowMapper.selectUserIds(warmFlowInteractiveTypeVo);
    }

    public String getName(String id) {
        Map<String, String> userMap = StreamUtils.toMap(angisUserService.selectUserList(new UserInput())
                , UserOutput::getId, UserOutput::getNickName);
        Map<String, String> deptMap = StreamUtils.toMap(deptService.selectDeptList(new DeptInput())
                , DeptOutput::getId, DeptOutput::getName);
        Map<String, String> roleMap = StreamUtils.toMap(roleService.all()
                , Role::getId, Role::getName);
        if (StringUtils.isNotBlank(id)) {
            if (id.contains("user:")) {
                String name = userMap.get(Long.valueOf(id.replace("user:", "")));
                if (StringUtils.isNotEmpty(name)) {
                    return "用户:" + name;
                }
            } else if (id.contains("dept:")) {
                String name = deptMap.get(Long.valueOf(id.replace("dept:", "")));
                if (StringUtils.isNotEmpty(name)) {
                    return "部门:" + name;
                }
            } else if (id.contains("role")) {
                String name = roleMap.get(Long.valueOf(id.replace("role:", "")));
                if (StringUtils.isNotEmpty(name)) {
                    return "角色:" + name;
                }
            } else {
                try {
//                    long parseLong = Long.parseLong(id);
                    String name = userMap.get(id);
                    if (StringUtils.isNotEmpty(name)) {
                        return "用户:" + name;
                    }
                } catch (NumberFormatException e) {
                    return id;
                }

            }
        }
        return "";
    }

    public Map<String, String> getNameMap(List<String> ids) {
        Map<String, String> userMap = StreamUtils.toMap(angisUserService.selectUserList(new UserInput())
                , UserOutput::getId, UserOutput::getNickName);
        Map<String, String> deptMap = StreamUtils.toMap(deptService.selectDeptList(new DeptInput())
                , DeptOutput::getId, DeptOutput::getName);
        Map<String, String> roleMap = StreamUtils.toMap(roleService.allNotDel()
                , Role::getId, Role::getName);
        Map<String, String> map = new HashMap<>();
        if (CollUtil.isNotEmpty(ids)) {
            for (String id : ids) {
                if (id.contains("user:")) {
                    String name = userMap.get(Long.valueOf(id.replace("user:", "")));
                    if (StringUtils.isNotEmpty(name)) {
                        map.put("用户", name);
                    }
                } else if (id.contains("dept:")) {
                    String name = deptMap.get(Long.valueOf(id.replace("dept:", "")));
                    if (StringUtils.isNotEmpty(name)) {
                        map.put("部门", name);
                    }
                } else if (id.contains("role")) {
                    String name = roleMap.get(Long.valueOf(id.replace("role:", "")));
                    if (StringUtils.isNotEmpty(name)) {
                        map.put("角色", name);
                    }
                } else {
                    try {
                        long parseLong = Long.parseLong(id);
                        String name = userMap.get(parseLong);
                        if (StringUtils.isNotEmpty(name)) {
                            map.put("用户", name);
                        }
                    } catch (NumberFormatException e) {
                        return MapUtil.newAndPut(id, id);
                    }

                }
            }
        }
        return map;
    }
}
